$(function(){
	//重置
	$('#btn-reset').click(function(){
		$('#search-userName').val("");
		$('#search-mail').val("");
		var p ={p:null}
		selectAllUserInfo(p);
	})
	//查询所有用户信息
	var p ={p:null}
	selectAllUserInfo(p);
	function selectAllUserInfo(p){
		  $('#user-order').empty();
		  $('#user-order').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>用户名</td><td>邮箱</td> <td>是否管理员</td></tr>');
	$.ajax({
		  type:"POST",
		  url:"/movie.front/userInfo/findAllUsersList",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){
           array = data.result;
          
           for(var i=0;i<array.length;i++){
        	   var html = '' ;
        	   if(array[i].isFlag==1){
        		   html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].userName+'</td><td>'+array[i].email+'</td><td>否</td></tr>';
           	    $('#user-order').append(html); 
        	   }else{
        		   html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].userName+'</td><td>'+array[i].email+'</td><td>是</td></tr>';
              	    $('#user-order').append(html); 
        	   }
        	   
           }
		   
		   
		  }
	
	   });
		
	}
	//查询信息
	$('#btn-search').click(function(){
    var userName =	$('#search-userName')[0].value;
	var mail = $('#search-mail')[0].value;
	 var p = {userName:userName,mail:mail};
	 selectAllUserInfo(p);
	})
	
	$('#btn-insert').click(function(){
		$('#userName').val("");
		$('#email').val("");
		  $('#isFlag')[0].value="";
		$('#myModal').modal('show');
		/*alert('aaaaaaa');*/
	})
	//添加用户信息
	$('#btn-save-user').click(function(){
	var userName =	$('#userName')[0].value;
	var id = $('#id')[0].value;
	var email = $('#email')[0].value;
	var isFlag = $('#isFlag')[0].value;
	if(id==null || id==""){
		var p={userName:userName,email:email,isFlag:isFlag};
	$.ajax({
		  type:"POST",
		  url:"/movie.front/userInfo/addUser",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){
            if(data.successResultInfo!=null){
            	//关闭对话框
            	$('#myModal').modal('hide');
            	//刷新表格
            	var d={d:null}
            	selectAllUserInfo(d);
            }
            if(data.errorResultInfo!=null){
            	alert(data.errorResultInfo.errorcode);
            }
		   
		  }
	
	   });
	}else{
		var p={id:id,userName:userName,email:email,isFlag:isFlag};
		$.ajax({
			  type:"POST",
			  url:"/movie.front/userInfo/updateUser",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				  
					  $('#myModal').modal('hide');
						//刷新表格
			            	var d={d:null}
			            	selectAllUserInfo(d);					  
				  

			   
			  }
		
		   });
	 }
	});
	
	//修改用户信息
	$('#btn-update-user').click(function(){
		var flag=0;
		var id =0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
			var id = array[i].id;
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	return false;
		      }else{
		    	  id=array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
			  $('#myModal').modal('show');
			  var id = {id:id};
			  getByIdUserInfo(id);
		     }
	});
	
	//删除用户信息
	$('#btn-remove-user').click(function(){
		var flag=0;
		var id=0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	 return false;
		      }else{
		    	  var id = array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
		    	 var id = {id:id};
		    	 deleteUserInfo(id);
		      }
		
	});
	function deleteUserInfo(p){
		$.ajax({
			  type:"POST",
			  url:"/movie.front/userInfo/deleteUser",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				  alert("删除成功");
				  var p ={p:null};
				  selectAllUserInfo(p);
				  
			     }
		   });
	}
	function getByIdUserInfo(p){
		//回显信息
	
		$.ajax({
			  type:"POST",
			  url:"/movie.front/userInfo/getUserById",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				  $('#id')[0].value=data.result.id;
	           $('#userName')[0].value=data.result.userName;
	           $('#email')[0].value=data.result.email;
	           $('#isFlag')[0].value=data.result.isFlag;
			   
			  }
		
		   });
	}
	
	//修改密码
	$('#btn-remove-password').click(function(){
		var flag=0;
		var id=0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	 return false;
		      }else{
		    	  var id = array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
		    	
		    	 updatePassword(id);
		      }
	});
	
	function updatePassword(p){
		var userId = p;
	
		$('#myPasswordModal').modal('show');
		$('#btn-save-password').click(function(){
			var newPassword = $('#newPassword')[0].value;
			var surePassword =$('#surePassword')[0].value;
			if(newPassword!=surePassword){
				alert("两次输入的密码不一致，请重新输入");
			}else{
				var id = userId;
				 var p = {id:id,newPassword:newPassword};
				updatePasswordById(p)
			}
		});
	}
	function updatePasswordById(p){
		$.ajax({
			  type:"POST",
			  url:"/movie.front/userInfo/updatePasswordById",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				 var result = data.success;
			     if(result>0){
			    	 $('#myPasswordModal').modal('hide');
			    	 var p ={p:null};
					  selectAllUserInfo(p); 
			     }
			  }
		   });
	}
 });
